<script setup>
import { onMounted } from 'vue'
import Edit from './components/Edit.vue'
import axios from 'axios'
import { ref } from 'vue'
// TODO: 列表渲染
const list=ref([])
const getlist=async()=>{
  const res=await axios.get('/list')
  list.value=res.data
}
onMounted(()=>
  getlist())

// TODO: 删除功能
//先获取id,调用删除按钮，然后调用getlist
const ondelete=async(id)=>{
 await axios.delete(`/del/${id}`).then(()=>{
    getlist()
  })
}


// TODO: 编辑功能
//打开弹框，回填数据，更新数据
const alert=ref(null)
const edit=(row)=>{
  alert.value.open(row)
}

</script>

<template>
  <div class="app">
    <el-table :data="list">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="姓名" prop="name" width="150"></el-table-column>
      <el-table-column label="籍贯" prop="place"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button type="primary" @click="edit(row)">编辑</el-button>
          <el-button type="danger" @click="ondelete(row.id)" link>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <!-- //子传父 -->
  <Edit ref="alert" @update="getlist" />
</template>

<style scoped>
.app {
  width: 980px;
  margin: 100px auto 0;
}
</style>
